﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using loginSQL.Util;
using loginSQL.DataView;
using loginSQL.Pages;

namespace loginSQL.Window.DataViewDetail
{
    public partial class ViewDetail : Form
    {
        private Users user;

        public ViewDetail(string userid)
        {
            InitializeComponent();
            user = new Users(userid,0);
            initData();
            initComboBox();
        }

        private void initComboBox()
        {
            foreach(loginSQL.Util.License lisensi in user.licenses)
            {
                combo_lisensi.Items.Add(lisensi.license_name + "("+lisensi.license_level_name+")");
            }
            if (user.licenses.Count > 0)
            {
                fillData(0);
            }
            else
            {
                group_lisensi.Enabled = false;
            }
            if(combo_lisensi.Items.Count >0)
                combo_lisensi.SelectedIndex = 0;            
        }

        private void fillPerpanjanganRating(int p)
        {
            datagrid_perpanjangan_rating.Rows.Clear();
            int count = 0;
            foreach (RatingRenewals rr in user.licenses[p].ratingrenewals)
            {
                addDataGridViewData(datagrid_perpanjangan_rating, rr.getData(),count);
                count++;
            }
        }

        private void fillKlasifikasiRating(int p)
        {
            dataGrid_klasifikasi.Rows.Clear();
            int count = 0;
            foreach ( RatingClassification rc in user.licenses[p].ratingclassification)
            {
                addDataGridViewData(dataGrid_klasifikasi, rc.getData(), count);
                count++;
            }
        }

        private void fillPerpanjangan(int p)
        {
            int count = 0;
            dataGrid_perpanjangan.Rows.Clear();
            foreach (Renewals r in user.licenses[p].renewals)
            {
                addDataGridViewData(dataGrid_perpanjangan, r.getData(),count);
                count++;
            }
        }

        private void fillTanggal(int p)
        {
            this.lbl_tgl_pengeluaran.Text = user.licenses[p].tanggal_pengeluaran;
            this.lbl_berlaku_hingga.Text = user.licenses[p].berlaku_hingga;
        }

        private void fillData(int i)
        {
            if (user.licenses.Count > i)
            {
                fillTanggal(i);
                fillPerpanjangan(i);
                fillKlasifikasiRating(i);
                fillPerpanjanganRating(i);
            }
        }


        private void initData()
        {
            this.txt_no.Text = user.nomor;
            this.txt_nama.Text = user.nama_pemegang;
            this.txt_ttl.Text = user.tempat_tanggal_lahir;
            this.txt_kebangsaan.Text = user.kebangsaan;
            this.txt_jeniskelamin.Text = user.jenis_kelamin;
            this.txt_alamat.Text = user.alamat;
            this.txt_hp.Text = user.handphone;
            this.txt_email.Text = user.email;                            
        }
        
        private void addDataGridViewData(DataGridView dg, List<string> data,int counter)
        {            
            Color backColor = Color.White;
            int count = dg.Rows.Count;
            DataGridViewRow row = new DataGridViewRow();

            DataGridViewCellStyle style = new DataGridViewCellStyle();
            if (count % 2 == 0)
                backColor = Color.GreenYellow;
            style.BackColor = backColor; // the color change           
            row.DefaultCellStyle = style;
            row.Height = 20;
            row.MinimumHeight = 20;       
            data.Insert(0,Convert.ToString(counter));
            row.CreateCells(dg, data.ToArray());
            dg.Rows.Add(row);
            //dataGridView1.Rows.InsertRange(0, row);
        }

        private void combo_lisensi_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selected = combo_lisensi.SelectedIndex;
            fillData(selected);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_login_Click(object sender, EventArgs e)
        {
            if (Constants.collections.GetBook((string)combo_lisensi.SelectedItem) != null)
            {
                Preview preview = new Preview(user.id, (String)combo_lisensi.SelectedItem, combo_lisensi.SelectedIndex);
                preview.Show();
            }
            else
            {
                MessageBox.Show("Template untuk lisensi " + combo_lisensi.SelectedItem.ToString() + " tidak ada", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
